home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / emacs_src_18_58.lha / emacs-18.58 / src / lread.c < prev    next >
C/C++ Source or Header  |  1992-05-23  |  33KB  |  1,331 lines

  1. /* Lisp parsing and input streams.
  2.    Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <sys/file.h>
  25. #undef NULL
  26. #include "config.h"
  27. #include "lisp.h"
  28.  
  29. #ifndef standalone
  30. #include "buffer.h"
  31. #include "paths.h"
  32. #endif
  33.  
  34. #ifdef lint
  35. #include <sys/inode.h>
  36. #endif /* lint */
  37.  
  38. #ifndef X_OK
  39. #define X_OK 01
  40. #endif
  41.  
  42. Lisp_Object Qread_char, Qget_file_char, Qstandard_input;
  43. Lisp_Object Qvariable_documentation, Vvalues, Vstandard_input;
  44.  
  45. /* non-zero if inside `load' */
  46. int load_in_progress;
  47.  
  48. /* Search path for files to be loaded. */
  49. Lisp_Object Vload_path;
  50.  
  51. /* File for get_file_char to read from.  Use by load */
  52. static FILE *instream;
  53.  
  54. /* When nonzero, read conses in pure space */
  55. static int read_pure;
  56.  
  57. /* For use within read-from-string (this reader is non-reentrant!!) */
  58. static int read_from_string_index;
  59. static int read_from_string_limit;
  60.  
  61. /* Handle unreading and rereading of characters.
  62.  Write READCHAR to read a character, UNREAD(c) to unread c to be read again. */
  63.  
  64. static int unrch;
  65.  
  66. static int readchar (readcharfun)
  67.      Lisp_Object readcharfun;
  68. {
  69.   Lisp_Object tem;
  70.   register struct buffer *inbuffer;
  71.   register int c, mpos;
  72.  
  73.   if (unrch >= 0)
  74.     {
  75.       c = unrch;
  76.       unrch = -1;
  77.       return c;
  78.     }
  79.   if (XTYPE (readcharfun) == Lisp_Buffer)
  80.     {
  81.       inbuffer = XBUFFER (readcharfun);
  82.  
  83.       if (BUF_PT (inbuffer) >= BUF_ZV (inbuffer))
  84.     return -1;
  85.       c = *(unsigned char *) BUF_CHAR_ADDRESS (inbuffer, BUF_PT (inbuffer));
  86.       SET_BUF_PT (inbuffer, BUF_PT (inbuffer) + 1);
  87.       return c;
  88.     }
  89.   if (XTYPE (readcharfun) == Lisp_Marker)
  90.     {
  91.       inbuffer = XMARKER (readcharfun)->buffer;
  92.       mpos = marker_position (readcharfun);
  93.  
  94.       if (mpos > BUF_ZV (inbuffer) - 1)
  95.     return -1;
  96.       c = *(unsigned char *) BUF_CHAR_ADDRESS (inbuffer, mpos);
  97.       if (mpos != BUF_GPT (inbuffer))
  98.     XMARKER (readcharfun)->bufpos++;
  99.       else
  100.     Fset_marker (readcharfun, make_number (mpos + 1),
  101.              Fmarker_buffer (readcharfun));
  102.       return c;
  103.     }
  104.   if (EQ (readcharfun, Qget_file_char))
  105.     return getc (instream);
  106.  
  107.   if (XTYPE (readcharfun) == Lisp_String)
  108.     {
  109.       register int c;
  110.       /* This used to be return of a conditional expression,
  111.      but that truncated -1 to a char on VMS.  */
  112.       if (read_from_string_index < read_from_string_limit)
  113.     c = XSTRING (readcharfun)->data[read_from_string_index++];
  114.       else
  115.     c = -1;
  116.       return c;
  117.     }
  118.  
  119.   tem = call0 (readcharfun);
  120.  
  121.   if (NULL (tem))
  122.     return -1;
  123.   return XINT (tem);
  124. }
  125.  
  126. #define READCHAR readchar(readcharfun)
  127. #define UNREAD(c) (unrch = c)
  128.  
  129. static Lisp_Object read0 (), read1 (), read_list (), read_vector ();
  130. static int read_escape ();
  131.  
  132. /* get a character from the tty */
  133.  
  134. DEFUN ("read-char", Fread_char, Sread_char, 0, 0, 0,
  135.   "Read a character from the command input (keyboard or macro).\n\
  136. It is returned as a number.")
  137.   ()
  138. {
  139.   register Lisp_Object val;
  140.  
  141. #ifndef standalone
  142.   XSET (val, Lisp_Int, read_command_char (0));
  143. #else
  144.   XSET (val, Lisp_Int, getchar ());
  145. #endif
  146.  
  147.   return val;
  148. }
  149.  
  150. DEFUN ("get-file-char", Fget_file_char, Sget_file_char, 0, 0, 0,
  151.   "Don't use this yourself.")
  152.   ()
  153. {
  154.   register Lisp_Object val;
  155.   XSET (val, Lisp_Int, getc (instream));
  156.   return val;
  157. }
  158.  
  159. static void readevalloop ();
  160. static Lisp_Object load_unwind ();
  161.  
  162. DEFUN ("load", Fload, Sload, 1, 4, 0,
  163.   "Execute a file of Lisp code named FILE.\n\
  164. First tries FILE with .elc appended, then tries with .el,\n\
  165.  then tries FILE unmodified.  Searches directories in  load-path.\n\
  166. If optional second arg NOERROR is non-nil,\n\
  167.  report no error if FILE doesn't exist.\n\
  168. Print messages at start and end of loading unless\n\
  169.  optional third arg NOMESSAGE is non-nil.\n\
  170. If optional fourth arg NOSUFFIX is non-nil, don't try adding\n\
  171.  suffixes .elc or .el to the specified name FILE.\n\
  172. Return t if file exists.")
  173.   (str, noerror, nomessage, nosuffix)
  174.      Lisp_Object str, noerror, nomessage, nosuffix;
  175. {
  176.   register FILE *stream;
  177.   register int fd = -1;
  178.   register Lisp_Object lispstream;
  179.   register FILE **ptr;
  180.   int count = specpdl_ptr - specpdl;
  181.   struct gcpro gcpro1;
  182.  
  183.   CHECK_STRING (str, 0);
  184.   str = Fsubstitute_in_file_name (str);
  185.  
  186.   /* Avoid weird lossage with null string as arg,
  187.      since it would try to load a directory as a Lisp file */
  188.   if (XSTRING (str)->size > 0)
  189.     {
  190.       fd = openp (Vload_path, str, !NULL (nosuffix) ? "" : ".elc:.el:", 0, 0);
  191.     }
  192.  
  193.   if (fd < 0)
  194.     if (NULL (noerror))
  195.       while (1)
  196.     Fsignal (Qfile_error, Fcons (build_string ("Cannot open load file"),
  197.                      Fcons (str, Qnil)));
  198.     else return Qnil;
  199.  
  200.   stream = fdopen (fd, "r");
  201.   if (stream == 0)
  202.     {
  203.       close (fd);
  204.       error ("Failure to create stdio stream for %s", XSTRING (str)->data);
  205.     }
  206.  
  207.   if (NULL (nomessage))
  208.     message ("Loading %s...", XSTRING (str)->data);
  209.  
  210.   GCPRO1 (str);
  211.   ptr = (FILE **) xmalloc (sizeof (FILE *));
  212.   *ptr = stream;
  213.   XSET (lispstream, Lisp_Internal_Stream, (int) ptr);
  214.   record_unwind_protect (load_unwind, lispstream);
  215.   load_in_progress++;
  216.   readevalloop (Qget_file_char, stream, Feval, 0);
  217.   unbind_to (count);
  218.   UNGCPRO;
  219.  
  220.   if (!noninteractive && NULL (nomessage))
  221.     message ("Loading %s...done", XSTRING (str)->data);
  222.   return Qt;
  223. }
  224.  
  225. static Lisp_Object
  226. load_unwind (stream)  /* used as unwind-protect function in load */
  227.      Lisp_Object stream;
  228. {
  229.   fclose (*(FILE **) XSTRING (stream));
  230.   free (XPNTR (stream));
  231.   if (--load_in_progress < 0) load_in_progress = 0;
  232.   return Qnil;
  233. }
  234.  
  235.  
  236. static int
  237. absolute_filename_p (pathname)
  238.      Lisp_Object pathname;
  239. {
  240.   register unsigned char *s = XSTRING (pathname)->data;
  241. #ifdef AMIGA
  242.   return (*s && index(s + 1, ':')); /* Non-leading : */
  243. #else
  244.   return (*s == '~' || *s == '/'
  245. #ifdef VMS
  246.       || index (s, ':')
  247. #endif /* VMS */
  248.       );
  249. #endif /* not AMIGA */
  250. }
  251.  
  252. /* Search for a file whose name is STR, looking in directories
  253.    in the Lisp list PATH, and trying suffixes from SUFFIX.
  254.    SUFFIX is a string containing possible suffixes separated by colons.
  255.    On success, returns a file descriptor.  On failure, returns -1.
  256.  
  257.    EXEC_ONLY nonzero means don't open the files,
  258.    just look for one that is executable.  In this case,
  259.    returns 1 on success.
  260.  
  261.    If STOREPTR is nonzero, it points to a slot where the name of
  262.    the file actually found should be stored as a Lisp string.
  263.    Nil is stored there on failure.  */
  264.  
  265. int
  266. openp (path, str, suffix, storeptr, exec_only)
  267.      Lisp_Object path, str;
  268.      char *suffix;
  269.      Lisp_Object *storeptr;
  270.      int exec_only;
  271. {
  272.   register int fd;
  273.   int fn_size = 100;
  274.   char buf[100];
  275.   register char *fn = buf;
  276.   int absolute = 0;
  277.   int want_size;
  278.   register Lisp_Object filename;
  279.   struct stat st;
  280.  
  281.   if (storeptr)
  282.     *storeptr = Qnil;
  283.  
  284.   if (absolute_filename_p (str))
  285.     absolute = 1;
  286.  
  287.   for (; !NULL (path); path = Fcdr (path))
  288.     {
  289.       char *nsuffix;
  290.  
  291.       filename = Fexpand_file_name (str, Fcar (path));
  292.       if (!absolute_filename_p (filename))
  293.     /* If there are non-absolute elts in PATH (eg ".") */
  294.     /* Of course, this could conceivably lose if luser sets
  295.        default-directory to be something non-absolute... */
  296.     {
  297.       filename = Fexpand_file_name (filename, current_buffer->directory);
  298.       if (!absolute_filename_p (filename))
  299.         /* Give up on this path element! */
  300.         continue;
  301.     }
  302.  
  303.       /* Calculate maximum size of any filename made from
  304.      this path element/specified file name and any possible suffix.  */
  305.       want_size = strlen (suffix) + XSTRING (filename)->size + 1;
  306.       if (fn_size < want_size)
  307.     fn = (char *) alloca (fn_size = 100 + want_size);
  308.  
  309.       nsuffix = suffix;
  310.  
  311.       /* Loop over suffixes.  */
  312.       while (1)
  313.     {
  314.       char *esuffix = (char *) index (nsuffix, ':');
  315.       int lsuffix = esuffix ? esuffix - nsuffix : strlen (nsuffix);
  316.  
  317.       /* Concatenate path element/specified name with the suffix.  */
  318.       strncpy (fn, XSTRING (filename)->data, XSTRING (filename)->size);
  319.       fn[XSTRING (filename)->size] = 0;
  320.       if (lsuffix != 0)  /* Bug happens on CCI if lsuffix is 0.  */
  321.         strncat (fn, nsuffix, lsuffix);
  322.  
  323.       /* Ignore file if it's a directory.  */
  324.       if (stat (fn, &st) >= 0
  325.           && (st.st_mode & S_IFMT) != S_IFDIR)
  326.         {
  327.           /* Check that we can access or open it.  */
  328.           if (exec_only)
  329.         fd = !access (fn, X_OK) ? 1 : -1;
  330.           else
  331.         fd = open (fn, 0, 0);
  332.  
  333.           if (fd >= 0)
  334.         {
  335.           /* We succeeded; return this descriptor and filename.  */
  336.           if (storeptr)
  337.             *storeptr = build_string (fn);
  338.           return fd;
  339.         }
  340.         }
  341.  
  342.       /* Advance to next suffix.  */
  343.       if (esuffix == 0)
  344.         break;
  345.       nsuffix += lsuffix + 1;
  346.     }
  347.       if (absolute) return -1;
  348.     }
  349.  
  350.   return -1;
  351. }
  352.  
  353.  
  354. Lisp_Object
  355. unreadpure ()    /* Used as unwind-protect function in readevalloop */
  356. {
  357.   read_pure = 0;
  358.   return Qnil;
  359. }
  360.  
  361. static void
  362. readevalloop (readcharfun, stream, evalfun, printflag)
  363.      Lisp_Object readcharfun;
  364.      FILE *stream;     
  365.      Lisp_Object (*evalfun) ();
  366.      int printflag;
  367. {
  368.   register int c;
  369.   register Lisp_Object val;
  370.   register int xunrch;
  371.   int count = specpdl_ptr - specpdl;
  372.  
  373.   specbind (Qstandard_input, readcharfun);
  374.  
  375.   unrch = -1;
  376.  
  377.   while (1)
  378.     {
  379.       instream = stream;
  380.       c = READCHAR;
  381.       if (c == ';')
  382.     {
  383.       while ((c = READCHAR) != '\n' && c != -1);
  384.       continue;
  385.     }
  386.       if (c < 0) break;
  387.       if (c == ' ' || c == '\t' || c == '\n' || c == '\f') continue;
  388.  
  389.       if (!NULL (Vpurify_flag) && c == '(')
  390.     {
  391.       record_unwind_protect (unreadpure, Qnil);
  392.       val = read_list (-1, readcharfun);
  393.       unbind_to (count + 1);
  394.     }
  395.       else
  396.     {
  397.       UNREAD (c);
  398.       val = read0 (readcharfun);
  399.     }
  400.  
  401.       xunrch = unrch;
  402.       unrch = -1;
  403.       val = (*evalfun) (val);
  404.       if (printflag)
  405.     {
  406.       Vvalues = Fcons (val, Vvalues);
  407.       if (EQ (Vstandard_output, Qt))
  408.         Fprin1 (val, Qnil);
  409.       else
  410.         Fprint (val, Qnil);
  411.     }
  412.       unrch = xunrch;
  413.     }
  414.  
  415.   unbind_to (count);
  416. }
  417.  
  418. #ifndef standalone
  419.  
  420. DEFUN ("eval-current-buffer", Feval_current_buffer, Seval_current_buffer, 0, 1, "",
  421.   "Execute the current buffer as Lisp code.\n\
  422. Programs can pass argument PRINTFLAG which controls printing of output:\n\
  423. nil means discard it; anything else is stream for print.")
  424.   (printflag)
  425.      Lisp_Object printflag;
  426. {
  427.   int count = specpdl_ptr - specpdl;
  428.   Lisp_Object tem;
  429.   if (NULL (printflag))
  430.     tem = Qsymbolp;
  431.   else
  432.     tem = printflag;
  433.   specbind (Qstandard_output, tem);
  434.   record_unwind_protect (save_excursion_restore, save_excursion_save ());
  435.   SET_PT (BEGV);
  436.   readevalloop (Fcurrent_buffer (), 0, Feval, !NULL (printflag));
  437.   unbind_to (count);
  438.   return Qnil;
  439. }
  440.  
  441. DEFUN ("eval-region", Feval_region, Seval_region, 2, 3, "r",
  442.   "Execute the region as Lisp code.\n\
  443. When called from programs, expects two arguments,\n\
  444. giving starting and ending indices in the current buffer\n\
  445. of the text to be executed.\n\
  446. Programs can pass third argument PRINTFLAG which controls printing of output:\n\
  447. nil means discard it; anything else is stream for print.")
  448.   (b, e, printflag)
  449.      Lisp_Object b, e, printflag;
  450. {
  451.   int count = specpdl_ptr - specpdl;
  452.   Lisp_Object tem;
  453.  
  454.   if (NULL (printflag))
  455.     tem = Qsymbolp;
  456.   else
  457.     tem = printflag;
  458.   specbind (Qstandard_output, tem);
  459.   if (NULL (printflag))
  460.     record_unwind_protect (save_excursion_restore, save_excursion_save ());
  461.   record_unwind_protect (save_restriction_restore, save_restriction_save ());
  462.   /* This both uses b and checks its type.  */
  463.   Fgoto_char (b);
  464.   Fnarrow_to_region (make_number (BEGV), e);
  465.   readevalloop (Fcurrent_buffer (), 0, Feval, !NULL (printflag));
  466.   unbind_to (count);
  467.   return Qnil;
  468. }
  469.  
  470. #endif standalone
  471.  
  472. DEFUN ("read", Fread, Sread, 0, 1, 0,
  473.   "Read one Lisp expression as text from STREAM, return as Lisp object.\n\
  474. If STREAM is nil, use the value of standard-input (which see).\n\
  475. STREAM or standard-input may be:\n\
  476.  a buffer (read from point and advance it)\n\
  477.  a marker (read from where it points and advance it)\n\
  478.  a function (call it with no arguments for each character)\n\
  479.  a string (takes text from string, starting at the beginning)\n\
  480.  t (read text line using minibuffer and use it).")
  481.   (readcharfun)
  482.      Lisp_Object readcharfun;
  483. {
  484.   extern Lisp_Object Fread_minibuffer ();
  485.  
  486.   unrch = -1;    /* Allow buffering-back only within a read. */
  487.  
  488.   if (NULL (readcharfun))
  489.     readcharfun = Vstandard_input;
  490.   if (EQ (readcharfun, Qt))
  491.     readcharfun = Qread_char;
  492.  
  493. #ifndef standalone
  494.   if (EQ (readcharfun, Qread_char))
  495.     return Fread_minibuffer (build_string ("Lisp expression: "), Qnil);
  496. #endif
  497.  
  498.   if (XTYPE (readcharfun) == Lisp_String)
  499.     return Fcar (Fread_from_string (readcharfun, Qnil, Qnil));
  500.  
  501.   return read0 (readcharfun);
  502. }
  503.  
  504. DEFUN ("read-from-string", Fread_from_string, Sread_from_string, 1, 3, 0,
  505.   "Read one Lisp expression which is represented as text by STRING.\n\
  506. Returns a cons: (OBJECT-READ . FINAL-STRING-INDEX).\n\
  507. START and END optionally delimit a substring of STRING from which to read;\n\
  508.  they default to 0 and (length STRING) respectively.")
  509.   (string, start, end)
  510.      Lisp_Object string, start, end;
  511. {
  512.   int startval, endval;
  513.   Lisp_Object tem;
  514.  
  515.   CHECK_STRING (string,0);
  516.  
  517.   if (NULL (end))
  518.     endval = XSTRING (string)->size;
  519.   else
  520.     { CHECK_NUMBER (end,2);
  521.       endval = XINT (end);
  522.       if (endval < 0 || endval > XSTRING (string)->size)
  523.     args_out_of_range (string, end);
  524.     }
  525.  
  526.   if (NULL (start))
  527.     startval = 0;
  528.   else
  529.     { CHECK_NUMBER (start,1);
  530.       startval = XINT (start);
  531.       if (startval < 0 || startval > endval)
  532.     args_out_of_range (string, start);
  533.     }
  534.  
  535.   read_from_string_index = startval;
  536.   read_from_string_limit = endval;
  537.  
  538.   unrch = -1;    /* Allow buffering-back only within a read. */
  539.  
  540.   tem = read0 (string);
  541.   return Fcons (tem, make_number (read_from_string_index));
  542. }
  543.  
  544. /* Use this for recursive reads, in contexts where internal tokens are not allowed. */
  545.  
  546. static Lisp_Object
  547. read0 (readcharfun)
  548.      Lisp_Object readcharfun;
  549. {
  550.   register Lisp_Object val;
  551.   char c;
  552.  
  553.   val = read1 (readcharfun);
  554.   if (XTYPE (val) == Lisp_Internal)
  555.     {
  556.       c = XINT (val);
  557.       return Fsignal (Qinvalid_read_syntax, Fcons (make_string (&c, 1), Qnil));
  558.     }
  559.  
  560.   return val;
  561. }
  562.  
  563. static int read_buffer_size;
  564. char *read_buffer;
  565.  
  566. static Lisp_Object
  567. read1 (readcharfun)
  568.      register Lisp_Object readcharfun;
  569. {
  570.   register int c;
  571.  
  572.  retry:
  573.  
  574.   c = READCHAR;
  575.   if (c < 0) return Fsignal (Qend_of_file, Qnil);
  576.  
  577.   switch (c)
  578.     {
  579.     case '(':
  580.       return read_list (0, readcharfun);
  581.  
  582.     case '[':
  583.       return read_vector (readcharfun);
  584.  
  585.     case ')':
  586.     case ']':
  587.     case '.':
  588.       {
  589.     register Lisp_Object val;
  590.     XSET (val, Lisp_Internal, c);
  591.     return val;
  592.       }
  593.  
  594.     case '#':
  595.       return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("#", 1), Qnil));
  596.  
  597.     case ';':
  598.       while ((c = READCHAR) >= 0 && c != '\n');
  599.       goto retry;
  600.  
  601.     case '\'':
  602.       {
  603.     return Fcons (Qquote, Fcons (read0 (readcharfun), Qnil));
  604.       }
  605.  
  606.     case '?':
  607.       {
  608.     register Lisp_Object val;
  609.  
  610.     XSET (val, Lisp_Int, READCHAR);
  611.     if (XFASTINT (val) == '\\')
  612.       XSETINT (val, read_escape (readcharfun));
  613.  
  614.     return val;
  615.       }
  616.  
  617.     case '\"':
  618.       {
  619.     register char *p = read_buffer;
  620.     register char *end = read_buffer + read_buffer_size;
  621.     register int c;
  622.     int cancel = 0;
  623.  
  624.     while ((c = READCHAR) >= 0 &&
  625.         (c != '\"' || (c = READCHAR) == '\"'))
  626.       {
  627.         if (p == end)
  628.           {
  629.         char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
  630.         p += new - read_buffer;
  631.         read_buffer += new - read_buffer;
  632.         end = read_buffer + read_buffer_size;
  633.           }
  634.         if (c == '\\')
  635.           c = read_escape (readcharfun);
  636.         /* c is -1 if \ newline has just been seen */
  637.         if (c < 0)
  638.           {
  639.         if (p == read_buffer)
  640.           cancel = 1;
  641.           }
  642.         else
  643.           *p++ = c;
  644.       }
  645.  
  646.     UNREAD (c);
  647.     /* If purifying, and string starts with \ newline,
  648.        return zero instead.  This is for doc strings
  649.        that we are really going to find in etc/DOC.nn.nn  */
  650.     if (!NULL (Vpurify_flag) && NULL (Vdoc_file_name) && cancel)
  651.       return make_number (0);
  652.  
  653.     if (read_pure)
  654.       return make_pure_string (read_buffer, p - read_buffer);
  655.     else
  656.       return make_string (read_buffer, p - read_buffer);
  657.       }
  658.  
  659.     default:
  660.       if (c <= 040)
  661.     goto retry;
  662.  
  663.       {
  664.     int quoted = 0;
  665.     register char *p = read_buffer;
  666.  
  667.     {
  668.       register char *end = read_buffer + read_buffer_size;
  669.  
  670.       while (c > 040 && 
  671.          !(c == '\"' || c == '\'' || c == ';' || c == '?'
  672.            || c == '(' || c == ')' || c =='.'
  673.            || c == '[' || c == ']' || c == '#'
  674.            ))
  675.         {
  676.           if (p == end)
  677.         {
  678.           register char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
  679.           p += new - read_buffer;
  680.           read_buffer += new - read_buffer;
  681.           end = read_buffer + read_buffer_size;
  682.         }
  683.           if (c == '\\')
  684.         {
  685.           quoted = 1;
  686.           c = READCHAR;
  687.         }
  688.           *p++ = c;
  689.           c = READCHAR;
  690.         }
  691.  
  692.       if (p == end)
  693.         {
  694.           char *new = (char *) xrealloc (read_buffer, read_buffer_size *= 2);
  695.           p += new - read_buffer;
  696.           read_buffer += new - read_buffer;
  697. /*          end = read_buffer + read_buffer_size;  */
  698.         }
  699.       *p = 0;
  700.       UNREAD (c);
  701.     }
  702.  
  703.     /* Is it an integer? */
  704.     {
  705.       register char *p1;
  706.       register Lisp_Object val;
  707.       p1 = read_buffer;
  708.       if (*p1 == '+' || *p1 == '-') p1++;
  709.       if (p1 != p && !quoted)
  710.         {
  711.           while (p1 != p && (c = *p1) >= '0' && c <= '9') p1++;
  712.           if (p1 == p)
  713.         /* It is. */
  714.         {
  715.           XSET (val, Lisp_Int, atoi (read_buffer));
  716.           return val;
  717.         }
  718.         }
  719.     }
  720.  
  721.     return intern (read_buffer);
  722.       }
  723.     }
  724. }
  725.  
  726. static Lisp_Object
  727. read_vector (readcharfun)
  728.      Lisp_Object readcharfun;
  729. {
  730.   register int i;
  731.   register int size;
  732.   register Lisp_Object *ptr;
  733.   register Lisp_Object tem, vector;
  734.   register struct Lisp_Cons *otem;
  735.   Lisp_Object len;
  736.  
  737.   tem = read_list (1, readcharfun);
  738.   len = Flength (tem);
  739.   vector = (read_pure ? make_pure_vector (XINT (len)) : Fmake_vector (len, Qnil));
  740.  
  741.  
  742.   size = XVECTOR (vector)->size;
  743.   ptr = XVECTOR (vector)->contents;
  744.   for (i = 0; i < size; i++)
  745.     {
  746.       ptr[i] = read_pure ? Fpurecopy (Fcar (tem)) : Fcar (tem);
  747.       otem = XCONS (tem);
  748.       tem = Fcdr (tem);
  749.       free_cons (otem);
  750.     }
  751.   return vector;
  752. }
  753.   
  754. /* flag = 1 means check for ] to terminate rather than ) and .
  755.    flag = -1 means check for starting with defun
  756.     and make structure pure.  */
  757.  
  758. static Lisp_Object
  759. read_list (flag, readcharfun)
  760.      int flag;
  761.      register Lisp_Object readcharfun;
  762. {
  763.   /* -1 means check next element for defun,
  764.      0 means don't check,
  765.      1 means already checked and found defun. */
  766.   int defunflag = flag < 0 ? -1 : 0;
  767.   Lisp_Object val, tail;
  768.   register Lisp_Object elt, tem;
  769.   struct gcpro gcpro1, gcpro2;
  770.  
  771.   val = Qnil;
  772.   tail = Qnil;
  773.  
  774.   while (1)
  775.     {
  776.       GCPRO2 (val, tail);
  777.       elt = read1 (readcharfun);
  778.       UNGCPRO;
  779.       if (XTYPE (elt) == Lisp_Internal)
  780.     {
  781.       if (flag > 0)
  782.         {
  783.           if (XINT (elt) == ']')
  784.         return val;
  785.           return Fsignal (Qinvalid_read_syntax, Fcons (make_string (") or . in a vector", 18), Qnil));
  786.         }
  787.       if (XINT (elt) == ')')
  788.         return val;
  789.       if (XINT (elt) == '.')
  790.         {
  791.           GCPRO2 (val, tail);
  792.           if (!NULL (tail))
  793.         XCONS (tail)->cdr = read0 (readcharfun);
  794.           else
  795.         val = read0 (readcharfun);
  796.           elt = read1 (readcharfun);
  797.           UNGCPRO;
  798.           if (XTYPE (elt) == Lisp_Internal && XINT (elt) == ')')
  799.         return val;
  800.           return Fsignal (Qinvalid_read_syntax, Fcons (make_string (". in wrong context", 18), Qnil));
  801.         }
  802.       return Fsignal (Qinvalid_read_syntax, Fcons (make_string ("] in a list", 11), Qnil));
  803.     }
  804.       tem = (read_pure && flag <= 0
  805.          ? pure_cons (elt, Qnil)
  806.          : Fcons (elt, Qnil));
  807.       if (!NULL (tail))
  808.     XCONS (tail)->cdr = tem;
  809.       else
  810.     val = tem;
  811.       tail = tem;
  812.       if (defunflag < 0)
  813.     defunflag = EQ (elt, Qdefun);
  814.       else if (defunflag > 0)
  815.     read_pure = 1;
  816.     }
  817. }
  818.  
  819. static int
  820. read_escape (readcharfun)
  821.      Lisp_Object readcharfun;
  822. {
  823.   register int c = READCHAR;
  824.   switch (c)
  825.     {
  826.     case 'a':
  827.       return 007;
  828.     case 'b':
  829.       return '\b';
  830.     case 'e':
  831.       return 033;
  832.     case 'f':
  833.       return '\f';
  834.     case 'n':
  835.       return '\n';
  836.     case 'r':
  837.       return '\r';
  838.     case 't':
  839.       return '\t';
  840.     case 'v':
  841.       return '\v';
  842.     case '\n':
  843.       return -1;
  844.  
  845.     case 'M':
  846.       c = READCHAR;
  847.       if (c != '-')
  848.     error ("Invalid escape character syntax");
  849.       c = READCHAR;
  850.       if (c == '\\')
  851.     c = read_escape (readcharfun);
  852.       return c | 0200;
  853.  
  854.     case 'C':
  855.       c = READCHAR;
  856.       if (c != '-')
  857.     error ("Invalid escape character syntax");
  858.     case '^':
  859.       c = READCHAR;
  860.       if (c == '\\')
  861.     c = read_escape (readcharfun);
  862.       if (c == '?')
  863.     return 0177;
  864.       return (c & 0200) | (c & 037);
  865.       
  866.     case '0':
  867.     case '1':
  868.     case '2':
  869.     case '3':
  870.     case '4':
  871.     case '5':
  872.     case '6':
  873.     case '7':
  874.       {
  875.     register int i = c - '0';
  876.     register int count = 0;
  877.     while (++count < 3)
  878.       {
  879.         if ((c = READCHAR) >= '0' && c <= '7')
  880.           {
  881.         i *= 8;
  882.         i += c - '0';
  883.           }
  884.         else
  885.           {
  886.         UNREAD (c);
  887.         break;
  888.           }
  889.       }
  890.     return i;
  891.       }
  892.     default:
  893.       return c;
  894.     }
  895. }
  896.  
  897. Lisp_Object Vobarray;
  898. Lisp_Object initial_obarray;
  899.  
  900. Lisp_Object
  901. check_obarray (obarray)
  902.      Lisp_Object obarray;
  903. {
  904.   while (XTYPE (obarray) != Lisp_Vector || XVECTOR (obarray)->size == 0)
  905.     {
  906.       /* If Vobarray is now invalid, force it to be valid.  */
  907.       if (EQ (Vobarray, obarray)) Vobarray = initial_obarray;
  908.  
  909.       obarray = wrong_type_argument (Qvectorp, obarray);
  910.     }
  911.   return obarray;
  912. }
  913.  
  914. static int hash_string ();
  915. Lisp_Object oblookup ();
  916.  
  917. Lisp_Object
  918. intern (str)
  919.      char *str;
  920. {
  921.   Lisp_Object tem;
  922.   int len = strlen (str);
  923.   Lisp_Object obarray = Vobarray;
  924.   if (XTYPE (obarray) != Lisp_Vector || XVECTOR (obarray)->size == 0)
  925.     obarray = check_obarray (obarray);
  926.   tem = oblookup (obarray, str, len);
  927.   if (XTYPE (tem) == Lisp_Symbol)
  928.     return tem;
  929.   return Fintern ((!NULL (Vpurify_flag)
  930.            ? make_pure_string (str, len)
  931.            : make_string (str, len)),
  932.           obarray);
  933. }
  934.  
  935. DEFUN ("intern", Fintern, Sintern, 1, 2, 0,
  936.   "Return the symbol whose name is STRING.\n\
  937. A second optional argument specifies the obarray to use;\n\
  938. it defaults to the value of  obarray.")
  939.   (str, obarray)
  940.      Lisp_Object str, obarray;
  941. {
  942.   register Lisp_Object tem, sym, *ptr;
  943.  
  944.   if (NULL (obarray)) obarray = Vobarray;
  945.   obarray = check_obarray (obarray);
  946.  
  947.   CHECK_STRING (str, 0);
  948.  
  949.   tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size);
  950.   if (XTYPE (tem) != Lisp_Int)
  951.     return tem;
  952.  
  953.   if (!NULL (Vpurify_flag))
  954.     str = Fpurecopy (str);
  955.   sym = Fmake_symbol (str);
  956.  
  957.   ptr = &XVECTOR (obarray)->contents[XINT (tem)];
  958.   if (XTYPE (*ptr) == Lisp_Symbol)
  959.     XSYMBOL (sym)->next = XSYMBOL (*ptr);
  960.   else
  961.     XSYMBOL (sym)->next = 0;
  962.   *ptr = sym;
  963.   return sym;
  964. }
  965.  
  966. DEFUN ("intern-soft", Fintern_soft, Sintern_soft, 1, 2, 0,
  967.   "Return the symbol whose name is STRING, or nil if none exists yet.\n\
  968. A second optional argument specifies the obarray to use;\n\
  969. it defaults to the value of  obarray.")
  970.   (str, obarray)
  971.      Lisp_Object str, obarray;
  972. {
  973.   register Lisp_Object tem;
  974.  
  975.   if (NULL (obarray)) obarray = Vobarray;
  976.   obarray = check_obarray (obarray);
  977.  
  978.   CHECK_STRING (str, 0);
  979.  
  980.   tem = oblookup (obarray, XSTRING (str)->data, XSTRING (str)->size);
  981.   if (XTYPE (tem) != Lisp_Int)
  982.     return tem;
  983.   return Qnil;
  984. }
  985.  
  986. Lisp_Object
  987. oblookup (obarray, ptr, size)
  988.      Lisp_Object obarray;
  989.      register char *ptr;
  990.      register int size;
  991. {
  992.   int hash, obsize;
  993.   register Lisp_Object tail;
  994.   Lisp_Object bucket, tem;
  995.  
  996.   if (XTYPE (obarray) != Lisp_Vector ||
  997.       (obsize = XVECTOR (obarray)->size) == 0)
  998.     {
  999.       obarray = check_obarray (obarray);
  1000.       obsize = XVECTOR (obarray)->size;
  1001.     }
  1002.   /* Combining next two lines breaks VMS C 2.3.  */
  1003.   hash = hash_string (ptr, size);
  1004.   hash %= obsize;
  1005.   bucket = XVECTOR (obarray)->contents[hash];
  1006.   if (XFASTINT (bucket) == 0)
  1007.     ;
  1008.   else if (XTYPE (bucket) != Lisp_Symbol)
  1009.     error ("Bad data in guts of obarray"); /* Like CADR error message */
  1010.   else for (tail = bucket; ; XSET (tail, Lisp_Symbol, XSYMBOL (tail)->next))
  1011.       {
  1012.     if (XSYMBOL (tail)->name->size == size &&
  1013.         !bcmp (XSYMBOL (tail)->name->data, ptr, size))
  1014.       return tail;
  1015.     else if (XSYMBOL (tail)->next == 0)
  1016.       break;
  1017.       }
  1018.   XSET (tem, Lisp_Int, hash);
  1019.   return tem;
  1020. }
  1021.  
  1022. static int
  1023. hash_string (ptr, len)
  1024.      unsigned char *ptr;
  1025.      int len;
  1026. {
  1027.   register unsigned char *p = ptr;
  1028.   register unsigned char *end = p + len;
  1029.   register unsigned char c;
  1030.   register int hash = 0;
  1031.  
  1032.   while (p != end)
  1033.     {
  1034.       c = *p++;
  1035.       if (c >= 0140) c -= 40;
  1036.       hash = ((hash<<3) + (hash>>28) + c);
  1037.     }
  1038.   return hash & 07777777777;
  1039. }
  1040.  
  1041. void
  1042. map_obarray (obarray, fn, arg)
  1043.      Lisp_Object obarray;
  1044.      int (*fn) ();
  1045.      Lisp_Object arg;
  1046. {
  1047.   register int i;
  1048.   register Lisp_Object tail;
  1049.   CHECK_VECTOR (obarray, 1);
  1050.   for (i = XVECTOR (obarray)->size - 1; i >= 0; i--)
  1051.     {
  1052.       tail = XVECTOR (obarray)->contents[i];
  1053.       if (XFASTINT (tail) != 0 && XTYPE (tail) == Lisp_Symbol)
  1054.     while (1)
  1055.       {
  1056.         (*fn) (tail, arg);
  1057.         if (XSYMBOL (tail)->next == 0)
  1058.           break;
  1059.         XSET (tail, Lisp_Symbol, XSYMBOL (tail)->next);
  1060.       }
  1061.     }
  1062. }
  1063.  
  1064. mapatoms_1 (sym, function)
  1065.      Lisp_Object sym, function;
  1066. {
  1067.   call1 (function, sym);
  1068. }
  1069.  
  1070. DEFUN ("mapatoms", Fmapatoms, Smapatoms, 1, 2, 0,
  1071.   "Call FUNCTION on every symbol in OBARRAY.\n\
  1072. OBARRAY defaults to the value of  obarray.")
  1073.   (function, obarray)
  1074.      Lisp_Object function, obarray;
  1075. {
  1076.   Lisp_Object tem;
  1077.  
  1078.   if (NULL (obarray)) obarray = Vobarray;
  1079.   obarray = check_obarray (obarray);
  1080.  
  1081.   map_obarray (obarray, mapatoms_1, function);
  1082.   return Qnil;
  1083. }
  1084.  
  1085. #define OBARRAY_SIZE 511
  1086.  
  1087. void
  1088. init_obarray ()
  1089. {
  1090.   Lisp_Object oblength;
  1091.   int hash;
  1092.   Lisp_Object *tem;
  1093.  
  1094.   XFASTINT (oblength) = OBARRAY_SIZE;
  1095.  
  1096.   Qnil = Fmake_symbol (make_pure_string ("nil", 3));
  1097.   Vobarray = Fmake_vector (oblength, make_number (0));
  1098.   initial_obarray = Vobarray;
  1099.   staticpro (&initial_obarray);
  1100.   /* Intern nil in the obarray */
  1101.   /* These locals are to kludge around a pyramid compiler bug. */
  1102.   hash = hash_string ("nil", 3);
  1103.   /* Separate statement here to avoid VAXC bug. */
  1104.   hash %= OBARRAY_SIZE;
  1105.   tem = &XVECTOR (Vobarray)->contents[hash];
  1106.   *tem = Qnil;
  1107.  
  1108.   Qunbound = Fmake_symbol (make_pure_string ("unbound", 7));
  1109.   XSYMBOL (Qnil)->function = Qunbound;
  1110.   XSYMBOL (Qunbound)->value = Qunbound;
  1111.   XSYMBOL (Qunbound)->function = Qunbound;
  1112.  
  1113.   Qt = intern ("t");
  1114.   XSYMBOL (Qnil)->value = Qnil;
  1115.   XSYMBOL (Qnil)->plist = Qnil;
  1116.   XSYMBOL (Qt)->value = Qt;
  1117.  
  1118.   /* Qt is correct even if CANNOT_DUMP.  loadup.el will set to nil at end.  */
  1119.   Vpurify_flag = Qt;
  1120.  
  1121.   Qvariable_documentation = intern ("variable-documentation");
  1122.  
  1123.   read_buffer_size = 100;
  1124.   read_buffer = (char *) malloc (read_buffer_size);
  1125. }
  1126.  
  1127. void
  1128. defsubr (sname)
  1129.      struct Lisp_Subr *sname;
  1130. {
  1131.   Lisp_Object sym;
  1132.   sym = intern (sname->symbol_name);
  1133.   XSET (XSYMBOL (sym)->function, Lisp_Subr, sname);
  1134. }
  1135.  
  1136. #ifdef NOTDEF /* use fset in subr.el now */
  1137. void
  1138. defalias (sname, string)
  1139.      struct Lisp_Subr *sname;
  1140.      char *string;
  1141. {
  1142.   Lisp_Object sym;
  1143.   sym = intern (string);
  1144.   XSET (XSYMBOL (sym)->function, Lisp_Subr, sname);
  1145. }
  1146. #endif NOTDEF
  1147.  
  1148. /* New replacement for DefIntVar; it ignores the doc string argument
  1149.    on the assumption that make-docfile will handle that.  */
  1150. /* Define an "integer variable"; a symbol whose value is forwarded
  1151.  to a C variable of type int.  Sample call: */
  1152.   /* DEFVARINT ("indent-tabs-mode", &indent_tabs_mode, "Documentation");  */
  1153.  
  1154. void
  1155. defvar_int (namestring, address, doc)
  1156.      char *namestring;
  1157.      int *address;
  1158.      char *doc;
  1159. {
  1160.   Lisp_Object sym;
  1161.   sym = intern (namestring);
  1162.   XSET (XSYMBOL (sym)->value, Lisp_Intfwd, address);
  1163. }
  1164.  
  1165. /* Similar but define a variable whose value is T if address contains 1,
  1166.  NIL if address contains 0 */
  1167.  
  1168. void
  1169. defvar_bool (namestring, address, doc)
  1170.      char *namestring;
  1171.      int *address;
  1172.      char *doc;
  1173. {
  1174.   Lisp_Object sym;
  1175.   sym = intern (namestring);
  1176.   XSET (XSYMBOL (sym)->value, Lisp_Boolfwd, address);
  1177. }
  1178.  
  1179. /* Similar but define a variable whose value is the Lisp Object stored at address. */
  1180.  
  1181. void
  1182. defvar_lisp (namestring, address, doc)
  1183.      char *namestring;
  1184.      Lisp_Object *address;
  1185.      char *doc;
  1186. {
  1187.   Lisp_Object sym;
  1188.   sym = intern (namestring);
  1189.   XSET (XSYMBOL (sym)->value, Lisp_Objfwd, address);
  1190.   staticpro (address);
  1191. }
  1192.  
  1193. /* Similar but don't request gc-marking of the C variable.
  1194.    Used when that variable will be gc-marked for some other reason,
  1195.    since marking the same slot twice can cause trouble with strings.  */
  1196.  
  1197. void
  1198. defvar_lisp_nopro (namestring, address, doc)
  1199.      char *namestring;
  1200.      Lisp_Object *address;
  1201.      char *doc;
  1202. {
  1203.   Lisp_Object sym;
  1204.   sym = intern (namestring);
  1205.   XSET (XSYMBOL (sym)->value, Lisp_Objfwd, address);
  1206. }
  1207.  
  1208. #ifndef standalone
  1209.  
  1210. /* Similar but define a variable whose value is the Lisp Object stored in
  1211.  the current buffer.  address is the address of the slot in the buffer that is current now. */
  1212.  
  1213. void
  1214. defvar_per_buffer (namestring, address, doc)
  1215.      char *namestring;
  1216.      Lisp_Object *address;
  1217.      char *doc;
  1218. {
  1219.   Lisp_Object sym;
  1220.   int offset;
  1221.   extern struct buffer buffer_local_symbols;
  1222.  
  1223.   sym = intern (namestring);
  1224.   offset = (char *)address - (char *)current_buffer;
  1225.  
  1226.   XSET (XSYMBOL (sym)->value, Lisp_Buffer_Objfwd,
  1227.     (Lisp_Object *) offset);
  1228.   *(Lisp_Object *)(offset + (char *)&buffer_local_symbols) = sym;
  1229.   if (*(int *)(offset + (char *)&buffer_local_flags) == 0)
  1230.     /* Did a DEFVAR_PER_BUFFER without initializing the corresponding
  1231.        slot of buffer_local_flags */
  1232.     abort ();
  1233. }
  1234.  
  1235. #endif standalone
  1236.  
  1237. init_read ()
  1238. {
  1239.   char *normal = PATH_LOADSEARCH;
  1240.   Lisp_Object normal_path;
  1241.  
  1242.   /* Compute the default load-path.  */
  1243. #ifndef CANNOT_DUMP
  1244.   /* If running a dumped Emacs in which load-path was set before dumping
  1245.      to a nonstandard value, use that value.  */
  1246.   if (initialized
  1247.       && !(XTYPE (Vload_path) == Lisp_Cons
  1248.        && XTYPE (XCONS (Vload_path)->car) == Lisp_String
  1249.        && !strcmp (XSTRING (XCONS (Vload_path)->car)->data, "../lisp")))
  1250.     normal_path = Vload_path;
  1251.   else
  1252. #endif
  1253.     normal_path = decode_env_path (0, normal);
  1254.  
  1255.   Vload_path = normal_path;
  1256.  
  1257.   /* Warn if dirs in the *standard* path don't exist.  */
  1258.   for (; !NULL (normal_path); normal_path = XCONS (normal_path)->cdr)
  1259.     {
  1260.       Lisp_Object dirfile;
  1261.       dirfile = Fcar (normal_path);
  1262.       if (!NULL (dirfile))
  1263.     {
  1264.       dirfile = Fdirectory_file_name (dirfile);
  1265.       if (access (XSTRING (dirfile)->data, 0) < 0)
  1266.         printf ("Warning: lisp library (%s) does not exist.\n",
  1267.             XSTRING (Fcar (normal_path))->data);
  1268.     }
  1269.     }
  1270.  
  1271.   if (egetenv ("EMACSLOADPATH"))
  1272.     Vload_path = decode_env_path ("EMACSLOADPATH", normal);
  1273. #ifndef CANNOT_DUMP
  1274.   if (!NULL (Vpurify_flag))
  1275.     Vload_path = Fcons (build_string ("../lisp"), Vload_path);
  1276. #endif                /* not CANNOT_DUMP */
  1277.  
  1278.   Vvalues = Qnil;
  1279.  
  1280.   load_in_progress = 0;
  1281. }
  1282.  
  1283. void
  1284. syms_of_read ()
  1285. {
  1286.   defsubr (&Sread);
  1287.   defsubr (&Sread_from_string);
  1288.   defsubr (&Sintern);
  1289.   defsubr (&Sintern_soft);
  1290.   defsubr (&Sload);
  1291.   defsubr (&Seval_current_buffer);
  1292.   defsubr (&Seval_region);
  1293.   defsubr (&Sread_char);
  1294.   defsubr (&Sget_file_char);
  1295.   defsubr (&Smapatoms);
  1296.  
  1297.   DEFVAR_LISP ("obarray", &Vobarray,
  1298.     "Symbol table for use by  intern  and  read.\n\
  1299. It is a vector whose length ought to be prime for best results.\n\
  1300. Each element is a list of all interned symbols whose names hash in that bucket.");
  1301.  
  1302.   DEFVAR_LISP ("values", &Vvalues,
  1303.     "List of values of all expressions which were read, evaluated and printed.\n\
  1304. Order is reverse chronological.");
  1305.  
  1306.   DEFVAR_LISP ("standard-input", &Vstandard_input,
  1307.     "Stream for read to get input from.\n\
  1308. See documentation of read for possible values.");
  1309.   Vstandard_input = Qt;
  1310.  
  1311.   DEFVAR_LISP ("load-path", &Vload_path,
  1312.     "*List of directories to search for files to load.\n\
  1313. Each element is a string (directory name) or nil (try default directory).\n\
  1314. Initialized based on EMACSLOADPATH environment variable, if any,\n\
  1315. otherwise to default specified in by file paths.h when emacs was built.");
  1316.  
  1317.   DEFVAR_BOOL ("load-in-progress", &load_in_progress,
  1318.     "Non-nil iff inside of  load.");
  1319.  
  1320.   Qstandard_input = intern ("standard-input");
  1321.   staticpro (&Qstandard_input);
  1322.  
  1323.   Qread_char = intern ("read-char");
  1324.   staticpro (&Qread_char);
  1325.  
  1326.   Qget_file_char = intern ("get-file-char");
  1327.   staticpro (&Qget_file_char);
  1328.  
  1329.   unrch = -1;
  1330. }
  1331.